/** 
 * Project Name:kaka 
 * File Name:AboutInfoServiceImple.java 
 * Package Name:com.luoran.kaka.service.impl 
 * Date:2018年12月3日下午4:10:12 
 * Copyright (c) 2018, Luoran, Inc. All Rights Reserved.
 * 
 */  
package com.luoran.kaka.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.luoran.kaka.dao.IAboutInfoDao;
import com.luoran.kaka.dao.IPicInfoDao;
import com.luoran.kaka.entity.AboutInfo;
import com.luoran.kaka.entity.PicInfo;
import com.luoran.kaka.service.IAboutInfoService;

/** 
 * ClassName: AboutInfoServiceImple
 * date: 2018年12月3日 下午4:10:12
 * @author ghl 
 * @since JDK 1.8
 */
@Service
public class AboutInfoServiceImple implements IAboutInfoService {
	
	public static final Logger log = LoggerFactory.getLogger(AboutInfoServiceImple.class);
	
	@Autowired
	private IAboutInfoDao aboutInfoDao;
	
	@Autowired
	private IPicInfoDao picInfoDao;

	/** 
	 * @see com.luoran.kaka.service.IAboutInfoService#saveAboutInfo(com.luoran.kaka.entity.AboutInfo) 
	 * @param aboutInfo
	 * @return
	 * @author ghl 2018年12月3日下午4:10:13
	 * saveAboutInfo:
	 */
	@Override
	public String saveAboutInfo(AboutInfo aboutInfo) {
		aboutInfoDao.insertTemplate(aboutInfo);
		return aboutInfo.getId();
	}

	/** 
	 * @see com.luoran.kaka.service.IAboutInfoService#getAboutInfoById(java.lang.String) 
	 * @param id
	 * @return
	 * @author ghl 2018年12月4日上午11:47:59
	 * getAboutInfoById:
	 */  
	@Override
	public List<Object> getAboutInfosById(String id) {
		log.info("根据基本信息id查询个人风采,参数为:{}",id);
		AboutInfo aboutInfo = null;
		PicInfo picInfo = null;
		String[] picIds = null;
		String url = null;
		String textContext = null;
		List<Object> list = new ArrayList<Object>();
		List<AboutInfo> aboutInfos = aboutInfoDao.getAboutInfosById(id);
		if(aboutInfos != null && aboutInfos.size()>0) {
			log.info("遍历个人风采集合获取详情");
			for (int i = 0; i < aboutInfos.size(); i++) {
				JSONObject obj =new JSONObject();
				List<String> listUrl = new ArrayList<String>();
				aboutInfo = aboutInfos.get(i);
				textContext = aboutInfo.getTextContext();
				if(!StringUtils.isEmpty(aboutInfo.getPicIds())) {
					picIds = aboutInfo.getPicIds().split(",");
					for (int j = 0; j < picIds.length; j++) {
						picInfo = picInfoDao.single(picIds[j]);
						if(null!=picInfo) {
							url = picInfo.getUrl();
							listUrl.add(url);
						}
					}
				}
				obj.put("textContext", textContext);
				obj.put("listUrl", listUrl);
				list.add(obj);
			}
		}
		return list;
	}

	/* 
	 * insertTemplate:新增风采信息
	 */
	@Override
	public String insertTemplate(AboutInfo aboutInfo) {
		aboutInfoDao.insertTemplate(aboutInfo);
		return aboutInfo.getId();
	}

}
